package com.kkd.kkdapp.db.app.model;

import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldMode;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.app.EADBHelper;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.user.model.UserInfo;
import com.kkd.kkdapp.utils.LogUtil;

import android.content.ContentValues;

/**
 * 当前用户登录基本信息
 * 
 */
public class User extends EABaseModel {
	private static final String TABLE_NAME = User.class.getSimpleName();

	// 用户ID
	@EADBField(mode = EADBFieldMode.NotNull, type = EADBFieldType.Text)
	public String customerId;

	// 手机号
	@EADBField (type = EADBFieldType.Text)
	public String mobile;

	// TODO 加密
	@EADBField
	public String pwd;

	// TODO 设备id，umeng推送使用
	@EADBField
	public String deviceId;

	// 客户端手机令牌
	@EADBField (type = EADBFieldType.Text)
	public String token;
	// 银行卡ID
	@EADBField
	public String cardId;
	// 登陆状态     0为已登陆  
	@EADBField (type = EADBFieldType.Integer)
	public int state = -1;

	// 实名认证  1为已认证   0为未认证
	@EADBField (type = EADBFieldType.Integer)
	public int isRealNameAuth;
	
	// 用户名和身份证号
	@EADBField (type = EADBFieldType.Text)
	public String idCard;
	
	// 用户名
	@EADBField (type = EADBFieldType.Text)
	public String realName;
	// 银行卡绑定    1为已绑定  0为未绑定
	@EADBField (type = EADBFieldType.Integer)
	public int isBoundBankCard;

	// 签到    1为已签到  0为未签到
	@EADBField (type = EADBFieldType.Integer)
	public int signInFlag = 0;
	// 签到 是否绑定手机  1为已绑定  0为未绑定
	@EADBField (type = EADBFieldType.Integer)
	public int isBoundPhone;

	public static long insert(User user) {
		User curUser = EADBHelper.getInstance().queryTopOne(User.class, null,
				null);
		if (curUser != null) {
			EADBHelper.getInstance().delete(TABLE_NAME, null, null);
		}

		return EADBHelper.getInstance().saveBindId(user);
	}

	public static User getCurUser() {
		User user = EADBHelper.getInstance()
				.queryTopOne(User.class, null, null);
		return user;
	}

	public static boolean delCurUser() {
		return EADBHelper.getInstance().delete(TABLE_NAME, null, null) > 0;
	}
	
	public static boolean updateCardId(String cardid) {
		ContentValues values = new ContentValues();
		values.put("cardid", cardid);
		return EADBHelper.getInstance().update(TABLE_NAME, values, null,null) > 0;
	}

	public static boolean updaterealName(String realName) {
		ContentValues values = new ContentValues();
		values.put("realName", realName);
		return EADBHelper.getInstance().update(TABLE_NAME, values, null,null) > 0;
	}
	public static boolean updateIsRealNameAuth(int isRealNameAuth) {
		ContentValues values = new ContentValues();
		values.put("isRealNameAuth", isRealNameAuth);
		return EADBHelper.getInstance().update(TABLE_NAME, values, null,null) > 0;
	}

	public void init(UserInfo userInfo) {
		customerId = userInfo.customerId;
		mobile = userInfo.mobile;
		pwd = userInfo.pwd;
		deviceId = userInfo.deviceId;
		token = userInfo.token;
		cardId = userInfo.cardId;
		state = userInfo.state;
		isRealNameAuth = userInfo.isRealNameAuth;
		idCard = userInfo.idCard;
		realName = userInfo.realName;
		isBoundBankCard = userInfo.isBoundBankCard;
		signInFlag = userInfo.signInFlag;
		isBoundPhone = userInfo.isBoundPhone;
	}
	
	
}
